package com.inspur.sc.springboot.controller;

import com.inspur.sc.springboot.utils.ResponseUtil;
import com.inspur.sc.springboot.utils.UploadUtil;
import com.inspur.sc.springboot.vo.ResponseVO;
import com.inspur.sc.springboot.vo.UploadFileVO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("api")
public class UploadController {

    /**
     * 文件上传
     * @param request
     * @return
     */
    @PostMapping("upload")
    public ResponseEntity<ResponseVO> upload(HttpServletRequest request) {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        List<UploadFileVO> fileList = UploadUtil.getUploadFiles(multiRequest);
        if(!fileList.isEmpty()){
            return ResponseUtil.success(fileList);
        }
        return ResponseUtil.success(null);
    }
}
